package com.lez.kotlincoroutine.activity

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.lez.kotlincoroutine.R
import com.lez.kotlincoroutine.databinding.ActivityMainTwoBinding
import com.lez.kotlincoroutine.viewmodel.MainViewModel

/**
 * 协程 + Retrofit + DataBinding + LiveData+ViewModel 案例
 */
class MainActivity02 : AppCompatActivity() {

    //懒加载形式初始化ViewModel
    private val mainViewModel: MainViewModel by viewModels()
    //mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val activityMainTwoBinding = DataBindingUtil.setContentView<ActivityMainTwoBinding>(this,R.layout.activity_main_two)
        activityMainTwoBinding.bannerViewModel = mainViewModel
        activityMainTwoBinding.lifecycleOwner = this

        activityMainTwoBinding.submitButton.also {
            it.setOnClickListener{
                mainViewModel.loadBanner()
            }
        }
    }

}